<?php
 /******************************************************************************
 *   GunCMS is an avid supporter of open source software. This is the appropriate 
 *   option if you are creating an open source application with a license 
 *   compatible with the GNU GPL license v3.
 *   For details http://www.gnu.org/licenses/gpl.html
 *   DO NOT USE FOR COMMERCIAL WITHOUT PERMISSION
 *
 * GunCMS Version 1.0.3
 * Copyright(c) 2009-2010, T & L GdbR
 * info@guncms.de
 * 
 * http://www.guncms.de
 ********************************************************************************/
class BWebshopInComingOrderBean {

    function BWebshopInComingOrderBean() {
    }	
    
    function get_all_incoming_order($db) {
		$sql = "SELECT o.so_id,o.payment_method,o.currency_value,u.customer_lastname,u.customer_firstname,u.customer_email ".
				" FROM " . TBL_ORDER_INFO." o ".
				" LEFT JOIN " . TBL_CUSTOMER." u ON u.customer_id = o.customer_id ";
    	$res = $db->get_results($sql);
    	
    	return $res;
	}
	
	function get_all_incoming_order_paging($db,$paging,$path,$sort,$by) {
    	if($sort!="") {
    		//Sort Field in Table Customer(u)
    		if ($sort == "customer_firstname" || $sort == "customer_email")
    			$condsort = "ORDER BY u.".$sort." ".$by;
    		else	
				$condsort = "ORDER BY o.".$sort." ".$by;
    	} else
			$condsort = "ORDER BY o.orders_date_finished DESC";		
		$page = isset ( $_GET[$path]) ? intval ( $_GET[$path] ) : 1;
        $rows_per_page= $paging;
        $page_start = ( $page - 1 ) * $rows_per_page;
        $page_end = $page * $rows_per_page;
		$sql = "SELECT o.so_id,o.customer_id,o.delivery_firstname, o.delivery_lastname, o.delivery_email_address, DATE_FORMAT(o.orders_date_finished, '%d.%m.%Y') as orders_date_finished,o.pay_status, ".
				"o.payment_method,c.symbol_left,c.symbol_right,o.currency_value,u.customer_lastname,u.customer_firstname,u.customer_email ".
				" FROM " . TBL_ORDER_INFO." o ".
				" LEFT JOIN " . TBL_CURRENCY." c ON c.currency_id = o.currency ".
				" LEFT JOIN " . TBL_CUSTOMER." u ON u.customer_id = o.customer_id  $condsort LIMIT $page_start,$rows_per_page";
    	$res = $db->get_results($sql);
    	
    	return $res;
	}
	
	public function get_incoming_order_by_id($db, $id) {
		$sql = "SELECT o.*,c.*,u.customer_lastname,u.customer_firstname,u.customer_email,customer_street_address,customer_city,customer_state, ".
				"(SELECT t.country_name FROM ".TBL_COUNTRY." t WHERE u.customer_country_id = t.country_id) as customer_country_name,".
				"(SELECT d.country_name FROM ".TBL_COUNTRY." d WHERE o.delivery_country = d.country_id) as delivery_country_name, ".
				"(SELECT b.country_name FROM ".TBL_COUNTRY." b WHERE o.billing_country = b.country_id) as billing_country_name ".
				" FROM " . TBL_ORDER_INFO . " o ".
			   " LEFT JOIN " . TBL_CURRENCY." c ON c.currency_id = o.currency ".
			   " LEFT JOIN " . TBL_CUSTOMER." u ON u.customer_id = o.customer_id ".
			   " WHERE o.so_id = '$id'";
    	$res = $db->get_row($sql);
    	return $res;
	}
	
	public function get_incoming_order_product($db, $id) {
		$sql = "SELECT * FROM " . TBL_ORDER_PRODUCT . " pr WHERE pr.so_id = '$id'";
    	$res = $db->get_results($sql);
    	return $res;
	}
	
	function validate_incoming_order() {
    	$msg = "";	
    	
		if(trim($_POST['code']) == "") {
			if($msg != "") $msg .= "<br />";
			$msg .= _("You must enter the code.");
		}
		
		if(trim($_POST['name']) == "") {
			if($msg != "") $msg .= "<br />";
			$msg .= _("You must enter the name.");
		}
		
		if(trim($_POST['delivery_cost']) == "") {
			if($msg != "") $msg .= "<br />";
			$msg .= _("You must enter the cost.");
		}
		
		if(trim($_POST['delivery_cost']) != "" && !is_numeric($_POST['delivery_cost'])) {
			if($msg != "") $msg .= "<br />";
			$msg .= _("Invalid cost, cost should be a numeric value.");
		}
		
		return $msg;
    }
	
	function save_incoming_order($db, $method) {
		$sql=sprintf(
					"INSERT INTO %s(code,name,delivery_cost,currency_id,status)".
					"VALUES('%s','%s','%s','%s','%s')",
    				TBL_ORDER_PRODUCT, 
    				$method['code'],
    				$method['name'],
    				$method['delivery_cost'],
					$method['currency_id'],
    				$method['status']);
    	$res = $db->query($sql);
		$id = -1;
		if($res == true) {	
			$id=$method['code'];
		}	
    	return $id;
	}
	
	function delete_incoming_order_by_id($db, $id) {
    	$sql = sprintf("DELETE FROM %1\$s WHERE so_id='$id'", TBL_ORDER_INFO);		
		$sqlproduct = sprintf("DELETE FROM %1\$s WHERE so_id='$id'", TBL_ORDER_PRODUCT);		
    	$res = $db->query($sql);
		$resproduct = $db->query($sqlproduct);
    	return $res;
    }
	
	function update_incoming_order($db, $order) {
		$sql = sprintf("UPDATE  %s SET delivery_street_address = '%s'," .
    					" delivery_city = '%s'," .
    					" delivery_postcode = '%s'," .
    					" delivery_state = '%s'," .
    					" delivery_country = '%s'," .
						" billing_street_address = '%s',".
						" billing_city = '%s'," .
    					" billing_postcode = '%s'," .
    					" billing_state = '%s'," .
    					" billing_country = '%s'" .
    					" WHERE so_id = '%s'", 
					TBL_ORDER_INFO,
					$order['delivery_street_address'], 
					$order['delivery_city'], 
					$order['delivery_postcode'],
					$order['delivery_state'],
					$order['delivery_country'],
					$order['billing_street_address'], 
					$order['billing_city'], 
					$order['billing_postcode'],
					$order['billing_state'],
					$order['billing_country'],
					$order['so_id']);
		$res = $db->query($sql);
    	return $res;
	}
	
	public function update_status_paid($db, $id, $value) {
		$sql = sprintf("UPDATE  %s SET pay_status = '%s'" .
    					" WHERE so_id = '%s'", 
					TBL_ORDER_INFO,
					$value,
					$id);
		$res = $db->query($sql);
    	return $res;
	}
}
?>